using AutoMapper;
using BaseData.CommonLib.Models;
using BaseData.DomainLib.AggregatesModel.SupplierAggregate;
using MediatR;
using Swimj.CommonLib.Base;

namespace BaseData.ApplicationLib.Commands
{
    public class CreateSupplierCommand : SupplierEditRequestDto, IRequest<Result<SupplierInfoResponseDto>>
    {

    }


    public class CreateSupplierCommandHandler : IRequestHandler<CreateSupplierCommand, Result<SupplierInfoResponseDto>>
    {
        private readonly ISupplierRepository _supplierRepository;
        private readonly IMapper _mapper;

        public CreateSupplierCommandHandler(ISupplierRepository supplierRepository,
        IMapper mapper)
        {
            this._supplierRepository = supplierRepository;
            this._mapper = mapper;
        }

        public async Task<Result<SupplierInfoResponseDto>> Handle(CreateSupplierCommand request, CancellationToken cancellationToken)
        {
            var supplier = new Supplier(request.Code, request.Name, request.Phone,
            request.Email, request.Address, request.Description);
            _supplierRepository.Add(supplier);

            await _supplierRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

            return await Result<SupplierInfoResponseDto>.SuccessAsync(_mapper.Map<SupplierInfoResponseDto>(supplier));
        }
    }
}